package com.example.neusoft.recyclerviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter mAdapter;

    private EditText ettitle,etdescript;
    private Button btnadd;

    List<ArticleBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initRecyclerView();
    }

    private void initRecyclerView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        list = getData();
        mAdapter = new MyAdapter(this);
        mAdapter.setList(list);
        recyclerView.setAdapter(mAdapter);
    }

    private void init() {
        ettitle=(EditText)findViewById(R.id.editText3);
        etdescript=(EditText)findViewById(R.id.editText4);
        btnadd=(Button)findViewById(R.id.button2);
        btnadd.setOnClickListener(this);
    }

    //获取数据源并形成List
    private List<ArticleBean> getData() {
        List<ArticleBean> list = new ArrayList<ArticleBean>();
        ArticleBean art;

        art = new ArticleBean();
        art.setArticleId(1);
        art.setTitle("标题1");
        art.setImgid(R.drawable.f1);
        art.setTime("2017-4-1 15:16");
        art.setDescription("描述1");
        list.add(art);

        art = new ArticleBean();
        art.setArticleId(2);

        art.setTitle("标题2");
        art.setImgid(R.drawable.f2);
        art.setTime("2017-4-2 15:16");
        art.setDescription("描述2");
        list.add(art);

        art = new ArticleBean();
        art.setArticleId(3);
        art.setTitle("标题3");
        art.setImgid(R.drawable.f3);
        art.setTime("2017-4-3 15:16");
        art.setDescription("描述3");
        list.add(art);

        art = new ArticleBean();
        art.setArticleId(4);
        art.setTitle("标题4");
        art.setImgid(R.drawable.f4);
        art.setTime("2017-4-4 15:16");
        art.setDescription("描述4");
        list.add(art);


        art = new ArticleBean();
        art.setArticleId(5);
        art.setTitle("标题5");
        art.setImgid(R.drawable.f5);
        art.setTime("2017-4-5 15:16");
        art.setDescription("描述5");
        list.add(art);

        return list;

    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.button2:
                //获取标题和描述
                String title=ettitle.getText().toString();
                String descript=etdescript.getText().toString();
                //获取当前时间
                String timecur = DateFormat.getDateTimeInstance().format(new Date());
                Toast.makeText(this, title+"\n"+descript+"\n"+timecur, Toast.LENGTH_SHORT).show();
                //给ArticleBean对象赋值，并追加到List中
                ArticleBean art=new ArticleBean();
                art.setArticleId(list.size()+1);
                art.setDescription(descript);
                art.setTime(timecur);
                art.setTitle(title);
                art.setImgid(R.mipmap.ic_launcher);//采用默认图片
                list.add(art);
                //给Adater传送list并刷新显示
                mAdapter.setList(list);
                mAdapter.notifyDataSetChanged();
                break;
        }
    }
}
